{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-27T06:18:19.854967900Z",
     "start_time": "2024-01-27T06:18:19.844469600Z"
    }
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'BinaryOperation' object has no attribute 'value'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[16], line 14\u001B[0m\n\u001B[0;32m     12\u001B[0m method \u001B[38;5;241m=\u001B[39m tree\u001B[38;5;241m.\u001B[39mtypes[\u001B[38;5;241m0\u001B[39m]\u001B[38;5;241m.\u001B[39mmethods[\u001B[38;5;241m0\u001B[39m]\n\u001B[0;32m     13\u001B[0m num \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlen\u001B[39m(method\u001B[38;5;241m.\u001B[39mbody[\u001B[38;5;241m0\u001B[39m]\u001B[38;5;241m.\u001B[39mexpression\u001B[38;5;241m.\u001B[39marguments)\n\u001B[1;32m---> 14\u001B[0m param1 \u001B[38;5;241m=\u001B[39m \u001B[43mmethod\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbody\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexpression\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43marguments\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mvalue\u001B[49m\n\u001B[0;32m     15\u001B[0m regex_param1 \u001B[38;5;241m=\u001B[39m re\u001B[38;5;241m.\u001B[39mescape(param1)\n\u001B[0;32m     16\u001B[0m code \u001B[38;5;241m=\u001B[39m re\u001B[38;5;241m.\u001B[39msub(\u001B[38;5;124mfr\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124m(\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124ms*?\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mregex_param1\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124ms*?,\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m(\u001B[39m\u001B[38;5;124m\"\u001B[39m, code, count\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m)  \u001B[38;5;66;03m# remove the first param\u001B[39;00m\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'BinaryOperation' object has no attribute 'value'"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import javalang\n",
    "\n",
    "code = '        assertEquals(961 * 31 + 1 * 31 + \"two\".hashCode() * 31 + Double.valueOf(3.0).hashCode(), ArrayUtils.hashCode(nonEmptyArray));'\n",
    "template = f\"\"\"\n",
    "public class Temp{{\n",
    "public void temp(){{\n",
    "    {code}\n",
    "}}\n",
    "}}\n",
    "\"\"\"\n",
    "tree = javalang.parse.parse(template)\n",
    "method = tree.types[0].methods[0]\n",
    "num = len(method.body[0].expression.arguments)\n",
    "param1 = method.body[0].expression.arguments[0].value\n",
    "regex_param1 = re.escape(param1)\n",
    "code = re.sub(fr'\\(\\s*?{regex_param1}\\s*?,', \"(\", code, count=1)  # remove the first param\n",
    "param1 = method.body[0].expression.arguments[1].value\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "'\",\" + \",\"'"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "import javalang\n",
    "from javalang.tree import Literal\n",
    "\n",
    "code = '        assertEquals(\",\" + \",\", ArrayUtils.hashCode(nonEmptyArray));'\n",
    "template = f\"\"\"\n",
    "public class Temp{{\n",
    "public void temp(){{\n",
    "    {code}\n",
    "}}\n",
    "}}\n",
    "\"\"\"\n",
    "tree = javalang.parse.parse(template)\n",
    "method = tree.types[0].methods[0]\n",
    "num = len(method.body[0].expression.arguments)\n",
    "arg_0 = method.body[0].expression.arguments[0]\n",
    "if isinstance(arg_0, Literal):\n",
    "    param1 = arg_0.value\n",
    "else:\n",
    "    param1 = re.search(r'\\(([^\\n]+?),\\s', code).group(1)\n",
    "regex_param1 = re.escape(param1)\n",
    "code = re.sub(fr'\\(\\s*?{regex_param1}\\s*?,', \"(\", code, count=1)  # remove the first param\n",
    "param1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-27T06:28:17.266015800Z",
     "start_time": "2024-01-27T06:28:17.251018600Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
